package com.tutu.thymeleaf.controller;

import com.tutu.thymeleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Program: SpringBoot
 * @Description: 表达式
 * @Author: JK.TuTu
 * @ @Version: 1.0
 * @Create: 2022-11-30 10:39
 **/
@Controller
@RequestMapping("thy")
public class TestController01 {
    /**
     * 标准变量表达式
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("test01")
    public String test1(HttpServletRequest request, Model model) {
        request.setAttribute("name", "张三");
        model.addAttribute("age", 18);
        User user = new User("张三", 18, new Date());
        model.addAttribute("user", user);
        //模板的逻辑名称  classpath:/templates/demo01.html
        return "demo01";
    }

    /**
     * 选择变量表达式
     *
     * @param model
     * @return
     */
    @GetMapping("test02")
    public String test2(Model model) {
        User user = new User("张三", 18, new Date());
        model.addAttribute("user", user);
        return "demo02";
    }

    /**
     * 链接表达式
     *
     * @param model
     * @return
     */
    @GetMapping("test03")
    public String test3(Model model) {
        model.addAttribute("id", 18);
        model.addAttribute("name", "张三");
        return "demo03";
    }


    /**
     * 链接表达式-没有参数的方法
     *
     * @return
     */
    @GetMapping("noParam")
    @ResponseBody
    public String noParam() {
        return "没有参数的方法";
    }

    /**
     * 链接表达式-一个参数的方法
     *
     * @return
     */
    @GetMapping("singletonParam")
    @ResponseBody
    public String singletonParam(int id) {
        return "一个参数的方法,id=" + id;
    }

    /**
     * 链接表达式-两个参数的方法
     *
     * @return
     */
    @GetMapping("MultiParam")
    @ResponseBody
    public String MultiParam(int id, String name) {
        return "两个参数的方法,id=" + id + ",name=" + name;
    }


}
